﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.Common;
using Mocola.BLL;
using Mocola.Web;
using Mocola.Model;
using Mocola.Common;
using Mocola.Security;

namespace Mocola.Net.Web.ajax
{
    public partial class ShowPwd : System.Web.UI.Page
    {
        private string info = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            string lb = Request.QueryString["lb"].ToString();
            string tid = Request.QueryString["tid"].ToString();
            string eid = Request.QueryString["eid"].ToString();
            //创建JavaScript
            info = "<script type=\"text/javascript\"> window.parent.MochaUI.updateContent({";
            info += "'element': window.parent.$('mainPanel'),";
            info += "'loadMethod': 'iframe',";
            if(lb == "P")
                info += "'url': 'mapfiles.aspx?tid=" + tid + "&eid=" + eid + "',";
            else if(lb == "M")
                info += "'url': 'mgfiles.aspx?tid=" + tid + "&eid=" + eid + "',";
            else if (lb == "Dept")
                info += "'url': 'departmentals.aspx',";
            info += "'title': 'CsLand Ams v1.0 - 中新置地档案管理系统',";
            info += "'scrollbars': false,";
            info += "'padding': { top: 0, right: 0, bottom: 0, left: 0 }";
            info += "});</script>";
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string pwd = "";
            CustomIdentity identity =  this.Page.User.Identity as CustomIdentity;

            if (identity != null)
            {
                // 获得identity中的值
                pwd = identity.UserPwd;
            }

            MD5Verifier md5 = new MD5Verifier();
            if (md5.verifyMd5Hash(txtPassword.Text.Trim(), pwd))
            {
                Del();
            }
            Response.Write("<script>window.parent.MochaUI.closeWindow(parent.$('delFileMap'));</script>");
        }

        private void Del()
        {
            string lb = Request.QueryString["lb"].ToString();
            string ZL = Request.QueryString["ZL"].ToString();
            string id = Request.QueryString["id"].ToString();

            DbTransaction transaction = Databases.MocolaNet.BeginTransaction(IsolationLevel.ReadCommitted);
            string sql = "";

            if (lb == "P")              //项目
            {
                if (ZL == "F")          //文件 
                {
                    sql = "DELETE FROM Moc_Files WHERE ID = " + id + ";";
                }
                else if (ZL == "M")     //图纸
                {
                    sql = "DELETE FROM Moc_Maps WHERE ID = " + id + ";";
                }
            }
            else if (lb == "M")         //管理
            {
                if (ZL == "F")          //文件 
                {
                    sql = "DELETE FROM Moc_MFiles WHERE ID = " + id + ";";
                }
                else if (ZL == "M")     //图纸
                {
                    sql = "DELETE FROM Moc_MMaps WHERE ID = " + id + ";";
                }
                else if (ZL == "O")     //图纸
                {
                    sql = "DELETE FROM Moc_OutFiles WHERE ID = " + id + ";";
                }
            }
            sql += "DELETE FROM Moc_Attachment WHERE ProjType = '" + lb + "' and FileType = '" + ZL + "' and RefID = " + id;
            if (lb == "Dept")
                sql = "DELETE FROM Moc_Departments WHERE ID = " + id;
            try
            {
                Databases.MocolaNet.ExecuteNonQuery(CommandType.Text, sql);
                Alert.Show("删除成功！");
                if (!Page.ClientScript.IsStartupScriptRegistered("UpdateParent"))
                {
                    //动态注册并执行JavaScript
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "UpdateParent", info);
                }
            }
            catch (Exception e)
            {
                Alert.Show("删除失败！" + e.ToString());
            }
        }
    }
}
